(function (G) {
  window["components_Input_Map_Point_Point"]({
  "component": true,
  "usingComponents": {
  }
},{
template:'#TEMPLATE_components_Input_Map_Point_Point',

    props: {
      name: {
        type: String,
        default:G.guid()
      },
      index: {
        type: Number
      },
      disabled: {
        type: Boolean,
        default: false
      },
      value: {
        type: String,
        default: '',
        observer: function (newValue, oldValue) {
          this.fliter(newValue);
        }
      },
      attr:{
        type:Object,
        default:{}
      }
    },
    data: function () {
      return {
        value1: [],
        map:'',
        markers:{}
      }
    },
    methods: {
      fliter(value){
        if(value == JSON.stringify(this.value1)) return
        //this.setData({ value1: G.string(value).parse([]) });
        if(this.map) {
          this.map.clearMap()
          //for(let key in this.markers) if(this.markers[key]) this.map.remove(this.markers[key])
          this.markers = {}
          let value1 = G.string(value).parse([])
          let attr = this.getData('attr') || {}
          attr.max = attr.max || 1
          if(value1.length > attr.max) value1 = value1.slice(value1.length - attr.max - 1,value1.length - 1)
          for(let i = 0; i < value1.length; i ++) {
            this.addMarker(value1[i])
          }
        }
      },
      addMarker(point){
        let attr = this.getData('attr') || {}
        attr.max = attr.max || 1
        return new Promise(next => {
          let markerConfig = this.getData('attr').marker || {}
          if(typeof markerConfig == 'function'){
            markerConfig(point,this.getData('name')).then(_markerConfig => {
              next(_markerConfig)
            })
          }else{
            next(markerConfig)
          }
        }).then(markerConfig => {
          return new Promise(next => {
            var marker = new AMap.Marker({
              position: new AMap.LngLat(point[0], point[1]),
              ...markerConfig
            })
            if(point[2]){
              marker.setTop(true)
              marker.setLabel({// label默认蓝框白底左上角显示，样式className为：amap-marker-label
                offset: new AMap.Pixel(0, 0), // 修改label相对于maker的位置
                content: point[2],
                direction: 'top'
              })
            }
            marker.on('click',event => {
              if(!this.getData('disabled')) {
                G.confirm('删除坐标点?').then(()=>{
                  let value1 = []
                  for(let i = 0; i < this.value1.length; i ++){
                    if(this.value1[i][0] != point[0] || this.value1[i][1] != point[1]) value1.push([this.value1[i][0], this.value1[i][1], this.value1[i][2] || ''])
                  }
                  this.value1 = value1
                  this.map.remove(marker)
                  delete this.markers[event.target._amap_id]
                  this.triggerEvent('change', { value: value1.length ? JSON.stringify(value1) : '',detail:{type:'markerDelete',address,longitude:point[0],latitude:point[1]}, id: this.getData('name'), index: this.getData('index') })
                })
              }else{
                this.triggerEvent('click', { event,type:'marker', id: this.getData('name'), index: this.getData('index') })
              }
            })
            if(attr.max == 1) {
              this.value1 = [point]
            }else{
              this.value1.push(point)
            }
            this.map.setCenter(point)
            this.markers[marker._amap_id] = marker
            this.map.add(marker)
            next()
          })
        })
      },
      addPonit(lon,lat,address){
        let attr = this.getData('attr') || {}
        attr.max = attr.max || 1
        if(attr.max == 1){
          this.addMarker([lon,lat,address]).then(() => {
            this.triggerEvent('change', { value: this.value1.length ? JSON.stringify(this.value1) : '',type:'markerMove',address,longitude:lon,latitude:lat, id: this.getData('name'), index: this.getData('index') })
          })
        }else{
          G.confirm('标记坐标点?').then(()=>{
            this.addMarker([lon,lat,address]).then(() => {
              this.triggerEvent('change', { value: this.value1.length ? JSON.stringify(this.value1) : '',type:'markerAdd',address,longitude:lon,latitude:lat, id: this.getData('name'), index: this.getData('index') })
            })
          })
        }
      }
    },
    mounted: function () {
      let attr = this.getData('attr') || {}
      attr.max = attr.max || 1
      let point = attr.center || [106.51107, 29.50197]
      this.map = new AMap.Map(this.getData('name'), {
        center:point,
        zoom: attr.zoom || 11
      })
      if(attr.max == 1) point.push('重庆市')
      if(this.value){
        this.fliter(this.value)
      }else if(attr.max == 1){
        let value1 = JSON.stringify([point])
        this.fliter(this.value || value1)
      }
      this.map.on('click', async event => {
        let point = [event.lnglat.lng,event.lnglat.lat]
        if(attr.geocodeRegeo && attr.max == 1){
          let res = await G.get(typeof attr.geocodeRegeo == 'string' ? attr.geocodeRegeo : '/v1/map/geocodeRegeo',{
            longitude:point[0],
            latitude:point[1]
          })
          let address = res.regeocode.formatted_address
          if(address) point.push(address)
        }
        if(!this.getData('disabled')) {
          if(attr.max == 1 || attr.max === undefined){
              let value1 = JSON.stringify([point])
              this.fliter(value1)
          }
          this.addPonit(point[0], point[1],point[2])
        }else{
          this.triggerEvent('click', { event,type:'map', id: this.getData('name'), index: this.getData('index') })
        }
      })
    }
  })
})(Y)
